package com.projles.util;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import com.projles.release1.R;

public class TocadorSons {
	
	private Context context;

	public static final int PRATO_ATAQUE = 0;
	public static final int TOM1 = 1;
	public static final int TOM2 = 2;
	public static final int PRATO_CONDUCAO = 3;
	public static final int SURDO = 4;
	public static final int BUMBO = 5;
	public static final int CAIXA = 6;
	public static final int CHIMBAL = 7;
	
	private float volume;
	private boolean vibra = true;
	
	private SoundPool soundPool;
	private HashMap<Integer, Integer> soundPoolMap;
	
	private static TocadorSons player;
	
	public static TocadorSons getPlayer(Context context) {
		if (player == null) {
			player = new TocadorSons(context);
		}
		player.setContext(context);
		return player;
	}

	private TocadorSons(Context context) {
		setContext(context);
		initSounds();
		volume = getVolume();
	}
	
	private void setContext(Context context) {
		this.context = context;
	}
	
	private Context getContext() {
		return this.context;
	}
	
	public void setMute(boolean mute) {
		if (mute) {
			volume = 0;
		} else {
		    volume = getVolume();
		}
	}
	
	public boolean isMuted() {
		return volume == 0;
	}
	
	public void setVibra(boolean vibra) {
		this.vibra = vibra;
	}
	
	public boolean isVibra() {
		return vibra;
	}
	
	private float getVolume() {
		AudioManager mgr = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
	    float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
	    float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);    
	    return streamVolumeCurrent / streamVolumeMax;
	}

	private void initSounds() {
	     soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
	     soundPoolMap = new HashMap<Integer, Integer>();
	     
	     soundPoolMap.put(PRATO_ATAQUE, soundPool.load(getContext(), R.raw.prato_de_ataque, 1));
	     soundPoolMap.put(TOM1, soundPool.load(getContext(), R.raw.tom1, 1));
	     soundPoolMap.put(TOM2, soundPool.load(getContext(), R.raw.tom2, 1));
	     soundPoolMap.put(PRATO_CONDUCAO, soundPool.load(getContext(), R.raw.prato_de_conducao, 1));
	     soundPoolMap.put(SURDO, soundPool.load(getContext(), R.raw.surdo, 1));
	     soundPoolMap.put(BUMBO, soundPool.load(getContext(), R.raw.bumbo, 1));
	     soundPoolMap.put(CAIXA, soundPool.load(getContext(), R.raw.caixa, 1));
	     soundPoolMap.put(CHIMBAL, soundPool.load(getContext(), R.raw.chimbal, 1));
	     
	}
	
	public void playSound(int sound, int prio) {
	    soundPool.play(soundPoolMap.get(sound), volume, volume, prio, 0, 1f);     
	}
	
	public void playSound(int sound) {
		playSound(sound, 1);    
	}
	
	public void chimbal() {
	    playSound(CHIMBAL);
	}
	
	public void pratoAtaque() {
	    playSound(PRATO_ATAQUE);
	}
	
	public void tom1() {
	    playSound(TOM1);
	} 
	
	public void tom2() {
	    playSound(TOM2);
	}
	
	public void pratoConducao() {
	    playSound(PRATO_CONDUCAO);
	}
	
	public void caixa() {
	    playSound(CAIXA);
	}
	
	public void bumbo() {
	    playSound(BUMBO);
	} 
	
	public void surdo() {
	    playSound(SURDO);
	} 

	
}
